class InstructorsController < ApplicationController
  before_filter :authenticate!
  before_filter :protect_from_student, :except => [:show]
  before_filter :protect_from_instructor, :only => [:new, :create, :destroy]
  before_filter :correct_user, :only => [:edit, :update, :cancellation_reports, :driving_lessons]
  # GET /instructors
  # GET /instructors.json
  def index
    @instructors = Instructor.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @instructors }
    end
  end

  # GET /instructors/1
  # GET /instructors/1.json
  def show
    @instructor = Instructor.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @instructor }
    end
  end

  # GET /instructors/new
  # GET /instructors/new.json
  def new
    @instructor = Instructor.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @instructor }
    end
  end

  # GET /instructors/1/edit
  def edit
    @instructor = Instructor.find(params[:id])
  end

  # POST /instructors
  # POST /instructors.json
  def create
    @instructor = Instructor.new(params[:instructor])

    respond_to do |format|
      if @instructor.save
        format.html { redirect_to @instructor, notice: 'Instructor was successfully created.' }
        format.json { render json: @instructor, status: :created, location: @instructor }
      else
        format.html { render action: "new" }
        format.json { render json: @instructor.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /instructors/1
  # PUT /instructors/1.json
  def update
    @instructor = Instructor.find(params[:id])

    respond_to do |format|
      if @instructor.update_attributes(params[:instructor])
        format.html { redirect_to @instructor, notice: 'Instructor was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @instructor.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /instructors/1
  # DELETE /instructors/1.json
  def destroy
    @instructor = Instructor.find(params[:id])
    @instructor.destroy

    respond_to do |format|
      format.html { redirect_to instructors_url }
      format.json { head :no_content }
    end
  end

  # GET /instructors/1/cancellation_reports
  def cancellation_reports
    @instructor = Instructor.find(params[:id])
    @cancellation_reports = @instructor.cancellation_reports

    respond_to do |format|
      format.html { render 'cancellation_reports/index'}
      format.json { render json: @cancellation_reports }
    end
  end

  # GET /instructors/1/driving_lessons
  def driving_lessons
    @instructor = Instructor.find(params[:id])
    @driving_lessons = @instructor.driving_lessons

    respond_to do |format|
      format.html { render 'driving_lessons/index'}
      format.json { render json: @driving_lessons }
    end
  end

  def correct_user
    @instructor = Instructor.find(params[:id])
    if @current_user.instructor?
      unless @current_user.usable.id == @instructor.id
        redirect_to actions_url, :alert => "Not authorized to view info about this instructor"
      end
    end
  end
end
